package cn.yiyuan.service.impl;

import cn.yiyuan.base.service.Imp.BaseServiceimpl;
import cn.yiyuan.domain.Dep;
import cn.yiyuan.mapper.DepMapper;
import cn.yiyuan.service.DepService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * @user m
 * @project system_boog
 * @date 2023-10-11
 * @package cn.yiyuan.service.impl
 */
//service注解
@Service
//事务注解
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class DepServiceImpl extends BaseServiceimpl<Dep> implements DepService {
//    注入DepMapper
    @Autowired
    private DepMapper depMapper;

    @Transactional
    @Override
    public void insert(Dep dep) {
        depMapper.insert(dep);
//        没有上级
        if (Objects.isNull(dep.getParent())){
            dep.setPath("/"+dep.getId());

        }else {
//            有上级
            Dep parent = depMapper.getById(dep.getParent().getId());
            dep.setPath(dep.getParent().getPath() + "/" + dep.getId());
        }
        depMapper.update(dep);
    }



    @Transactional
    @Override
    public void update(Dep dep) {

//        没有上级
        if (Objects.isNull(dep.getParent().getId())){
            dep.setPath("/"+dep.getId());
        }else {
//            有上级
            Dep parent = depMapper.getById(dep.getParent().getId());
            dep.setPath(dep.getParent().getPath() + "/" + dep.getId());
            depMapper.update(dep);
        }
        depMapper.update(dep);
    }



    @Override
    public List<Dep> getParent() {
        return depMapper.getParent();
    }
}
